package com.lj.universalframework.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.lj.universalframework.R


class BottomDialogFragment : BottomSheetDialogFragment() {

    private var mBehavior: BottomSheetBehavior<*>? = null

    companion object {
        @Volatile
        private var instance: BottomDialogFragment? = null

        fun getInstance() =
            instance ?: synchronized(this) {
                instance ?: BottomDialogFragment().also { instance = it }
            }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //设置背景透明
        setStyle(STYLE_NORMAL, R.style.dialog)

        val dialog: BottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        val view: View = View.inflate(context, R.layout.dottom_frggment_dialog, null)

        view.apply {
            findViewById<TextView>(R.id.tv_shut_down).apply {
                setOnClickListener {
                    mBehavior!!.state = BottomSheetBehavior.STATE_HIDDEN
                }
            }
        }
        val cl=view.findViewById<ConstraintLayout>(R.id.cl_dottom_dialog);
        mBehavior = BottomSheetBehavior.from(cl)
        return dialog
    }

    override fun onStart() {
        super.onStart()
        //全屏展开
        mBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
    }

    /**
     * 点击布局里的ImageView，触发的点击事件
     * @param v
     */
    fun doclick(v: View?) {
        mBehavior!!.state = BottomSheetBehavior.STATE_HIDDEN
    }
}